package com.mhg.socket;

import java.net.*;
import java.io.*;
		/*
			需求 客户端键盘录入 服务器端将字母转成大写 并发送会给客户端
				要求 使用 缓冲区技术
				就是建立一个文本转换服务器
				abcd over结束
				ABCD
			分析
				从客户端角度考虑 1.键盘录入 2.发送给服务端 3.从服务器端接收反馈
				从服务端角度考虑 1.接收 2.转大写 3.发回去
		*/
//客户端
public class Demo_03_Tcp_Client 
{
	public static void main(String[] args) throws Exception
	{
		//1.创建Socket
		Socket socket = new Socket("127.0.0.1",10013);
		//2.键盘录入
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		//3.向服务端发送 
		OutputStream out = socket.getOutputStream();
		BufferedWriter bwOut = new BufferedWriter(new OutputStreamWriter(out));
		//4.从服务器端接受反馈
		InputStream in = socket.getInputStream();
		BufferedReader brIn = new BufferedReader(new InputStreamReader(in));

		String line = null;
		while((line = br.readLine())!=null){
			if ("over".equals(line))
			{
				break;
			}
			bwOut.write(line);
			bwOut.newLine();
			bwOut.flush();

			//获取服务器端 返回的大写
			String str = brIn.readLine();
			System.out.println(str);
		}
	}
}
//服务端
class Demo_03_Tcp_Server
{
	public static void main(String[] args) throws Exception{
		//1.创建服务端对象
		ServerSocket ss = new ServerSocket(10013);
		Socket socket = ss.accept();
		
		InputStream in = socket.getInputStream();
		BufferedReader brIn = new BufferedReader(new InputStreamReader(in));

		OutputStream out = socket.getOutputStream();
		BufferedWriter bwOut = new BufferedWriter(new OutputStreamWriter(out));

		String line = null;
		while((line = brIn.readLine())!=null){
			System.out.println(line);
			bwOut.write(line.toUpperCase());
			bwOut.newLine();
			bwOut.flush();
		}
	}
}
